//
//  SCMainTabBar.m
//  Mutal
//
//  Created by TonyStart on 16/10/18.
//  Copyright © 2016年 com.wx. All rights reserved.
//

#import "SCMainTabBar.h"
#import "SCHomeController.h"
#import "SCMessageController.h"
#import "SCPersonCenterController.h"
#import "SCHopeController.h"
#import "SCLoginController.h"
@interface SCMainTabBar ()<UITabBarControllerDelegate>

@end


@implementation SCMainTabBar

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.delegate = self;
    NSLog(@"%@",NSHomeDirectory());
    SCHomeController *homeVC = [UIStoryboard storyboardWithName:@"HomeStoryboard" bundle:nil].instantiateInitialViewController;
//    homeVC.aaClick = ^{
//        self.selectedIndex = 1;
//    };
    SCMessageController *messageVC = [[SCMessageController alloc] init];
    SCPersonCenterController *personCenterVC = [UIStoryboard storyboardWithName:@"PersonStoryBoard" bundle:nil].instantiateInitialViewController;
  
    SCHopeController *hopeVC = [[SCHopeController alloc] init];
    
    //设置tabbar选中颜色和未选中颜色
//    self.tabBar.unselectedItemTintColor = [UIColor grayColor];
    self.tabBar.tintColor = [UIColor colorWithRed:81/255.0 green:200/255.0 blue:195/255.0 alpha:1];
    
    [self addChildViewController:homeVC title:@"主页" imageName:@"icon-首页"];
    [self addChildViewController:hopeVC title:@"项目" imageName:@"icon-项目"];
    [self addChildViewController:messageVC title:@"消息" imageName:@"icon-消息"];
    [self addChildViewController:personCenterVC title:@"我的" imageName:@"icon-我的"];
    
}



-(void)addChildViewController:(UIViewController*)vc title:(NSString*)title imageName:(NSString*)imgName{
    
    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:vc];
    vc.tabBarItem.title = title;
    nav.navigationBar.barTintColor = [UIColor colorWithRed:81/255.0 green:200/255.0 blue:195/255.0 alpha:1];
    // 设置图片原色绘制
    vc.tabBarItem.selectedImage = [[UIImage imageNamed:[NSString stringWithFormat:@"%@2",imgName]] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    
    vc.tabBarItem.image = [UIImage imageNamed:imgName];
    
    [self addChildViewController:nav];
}


-(void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController{
    UIViewController *currVC = viewController.childViewControllers.firstObject;
    
    if ([currVC isKindOfClass:[SCMessageController class]]) {
        BOOL isLogin = [[NSUserDefaults standardUserDefaults] objectForKey:@"is_login"];
        
        if (!isLogin) {
            SCLoginController *loginVC = [UIStoryboard storyboardWithName:@"LoginStoryboard" bundle:nil].instantiateInitialViewController;
            
            
            [currVC.navigationController pushViewController:loginVC animated:NO];
            
        }
        
    }
    
}





@end
